package me.zzp.domain;

import com.google.gson.Gson;
import lombok.Data;

import java.util.LinkedList;
import java.util.List;

/**
 * @author zhangzepeng
 */
@Data
public class Pageable {
    private final static Gson gson = new Gson();

    private boolean paging = false;
    private int page = 0;
    private int size = 0;
    private List<Order> sort = new LinkedList<>();

    public Pageable() {
    }

    public Pageable(int page, int size) {
        this.page = page;
        this.size = size;
        this.paging = size > 0;
    }

    public Pageable(int page, int size, List<Order> sort) {
        this.page = page;
        this.size = size;
        this.sort = sort;
        this.paging = size > 0;
    }

    public Pageable ascend(String column) {
        return orderBy(column, Order.Direction.asc);
    }

    public Pageable descend(String column) {
        return orderBy(column, Order.Direction.desc);
    }

    public Pageable orderBy(String column, String direction) {
        return orderBy(column, (direction.equalsIgnoreCase("desc")? Order.Direction.desc: Order.Direction.asc));
    }

    public Pageable orderBy(String column, Order.Direction direction) {
        sort.add(new Order(column, direction));
        return this;
    }

    public int getOffset() {
        return page * size;
    }

    @Override
    public String toString() {
        return gson.toJson(this);
    }
}
